package com.kpmg.datalake.web.config;

import com.kpmg.datalake.web.filter.CookieFilter;
import com.kpmg.datalake.web.interceptor.SessionInterceptor;
import javax.servlet.Filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Session配置
 * @author Tony
 */
@Configuration
@Order(Integer.MIN_VALUE)
public class SessionConfig extends WebMvcConfigurerAdapter {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
      registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/**")
          .excludePathPatterns("/swagger/*", "/api-docs/*", "/api/*", "/userManager/checkLogin",
              "/userManager/getDbInfo", "/plsql/**", "/pkgauto/getVersionInfo",
              "/pkgauto/downloadVersion", "/pkgauto/pkgDownload", "/userManager/getSaltForUser");
    }


    /**
     * 配置过滤器
     *
     * @return FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(cookieFilter());
        registration.addUrlPatterns("/*");
        registration.setName("cookieFilter");
        registration.setOrder(Integer.MIN_VALUE);

        return registration;
    }

    /**
     * 创建一个bean
     *
     * @return Filter
     */
    @Bean(name = "cookieFilter")
    public Filter cookieFilter() {
        return new CookieFilter();
    }

}
